<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/4/7
 * Time: 18:33
 */

namespace common\logics\pepperrecord;

use Yii;
use common\widgets\Tool;
use yii\base\BaseObject;

class PepperRecordLogic extends BaseObject implements IPepperRecord
{
    /** @var \common\sources\read\businesspepperrecord\BusinessPepperRecordAccess  $RBusinessPepperRecordAccess ***/
    private $RBusinessPepperRecordAccess;
    /** @var \common\sources\read\buyuser\BuyUserAccess $RBuyUserAccess ***/
    private $RBuyUserAccess;
    /** @var \common\sources\read\buyuser\BuyUserAcountAccess $RBuyUserAcountAccess ***/
    private $RBuyUserAcountAccess;
    /** @var \common\sources\write\businessacount\BusinessAcountAccess  $WBusinessAcountAccess **/
    private $WBusinessAcountAccess;
    /** @var \common\sources\write\businessdepositrecord\BusinessDepositRecordAccess  $WBusinessDepositRecordAccess **/
    private $WBusinessDepositRecordAccess;
    /** @var \common\sources\write\businesspepperrecord\BusinessPepperRecordAccess $WBusinessPepperRecordAccess **/
    private $WBusinessPepperRecordAccess;

    public function init()
    {
        $this->RBusinessPepperRecordAccess = Yii::$container->get("RBusinessPepperRecordAccess");
        $this->RBuyUserAccess = Yii::$container->get("RBuyUserAccess");
        $this->RBuyUserAcountAccess = Yii::$container->get("RBuyUserAcountAccess");
        $this->WBusinessAcountAccess = Yii::$container->get("WBusinessAcountAccess");
        $this->WBusinessDepositRecordAccess = Yii::$container->get("WBusinessDepositRecordAccess");
        $this->WBusinessPepperRecordAccess = Yii::$container->get("WBusinessPepperRecordAccess");
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function getPepperRecord($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家用户id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],402,"安全码不能为空");
        }
        if($data['safetyCode'] != md5($data['bus_id']))
        {
            return Tool::returnJson([],403,"安全码不正确");
        }
        $business = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($business))
        {
            return Tool::returnJson([],405,"商家账号不存在");
        }

        $count = $this->RBusinessPepperRecordAccess->getCount($data);
        $list = [];
        if($count >0)
        {
            $list = $this->RBusinessPepperRecordAccess->getList($data);
            foreach ($list as &$item)
            {
                $item['task_id'] = empty($item['task_id']) ? $item['remark'] : ( empty($item['task_order_num'])?$item['task_id']:$item['task_order_num']);
                $item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s',$item['create_time']): "";
            }
        }
        return Tool::returnJson([
            "list" => $list,
            "page_num" => $data['page_num'],
            "count" => $count,
            "limit" => $data['limit'],
        ],200,"成功");
    }
    //充值花椒记录
    public function getRechargePepperRecord($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家用户id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],402,"安全码不能为空");
        }
        if($data['safetyCode'] != md5($data['bus_id']))
        {
            return Tool::returnJson([],403,"安全码不正确");
        }
        $business = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($business))
        {
            return Tool::returnJson([],405,"商家账号不存在");
        }
        $count = $this->RBusinessPepperRecordAccess->getRechargeCount($data);
        $list = [];
        if($count >0)
        {
            $list = $this->RBusinessPepperRecordAccess->getRechargeList($data);
            foreach ($list as &$item)
            {
                $item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s',$item['create_time']): "";
            }
        }
        return Tool::returnJson([
            "list" => $list,
            "page_num" => $data['page_num'],
            "count" => $count,
            "limit" => $data['limit'],
        ],200,"成功");


    }


    //充值花椒
    public function rechargePepper($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家用户id不能为空");
        }
        if(empty($data['money']))
        {
            return Tool::returnJson([],402,"充值金额不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['bus_id'].$data['money']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        $business = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($business))
        {
            return Tool::returnJson([],406,"商家账号不存在");
        }
        $business_account = $this->RBuyUserAcountAccess->getBuyUserAccount($data['bus_id']);
        if(empty($business_account))
        {
            return Tool::returnJson([],407,"商家账号中没有可用押金");
        }
        if($business_account['ava_deposit'] - $data['money'] <0)
        {
            return Tool::returnJson([],408,"商家账号可用押金不足，请先充值");
        }
        //修改 商家账号记录
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $bus_arr =[
                  "total_deposit" => number_format($business_account['total_deposit'] - $data['money'],2,".",""),
                  "ava_deposit" => number_format($business_account['ava_deposit'] - $data['money'],2,".",""),
                  "ava_pepper" => number_format($business_account['ava_pepper'] + $data['money'],2,".",""),
                 "bus_id" => $data['bus_id']
            ];
            $res_bus = $this->WBusinessAcountAccess->rechargePepper($bus_arr);
            if(!$res_bus)
            {
                $transaction->rollBack();
                return Tool::returnJson([],409,"商家充值花椒失败");
            }

            $deposit_arr = [
                "bus_id" => $data['bus_id'],
                "expend" => number_format($data['money'],2,".",""),
                "income" => 0,
                "type" => 2,
                "balance" => number_format($business_account['ava_deposit']- $data['money'],2,".",""),
                "remark" => "商家充值花椒"
            ];
            $deposit_res = $this->WBusinessDepositRecordAccess->addDepositRecord($deposit_arr);
            if(!$deposit_res)
            {
                $transaction->rollBack();
                return Tool::returnJson([],410,"增加押金记录失败");
            }
            //增加充值花椒记录
            $pepper_arr = [
                "bus_id" => $data['bus_id'],
                "expend" => 0,
                "type" => 1,
                "income" =>  number_format($data['money'],2,".",""),
                "balance" => number_format($business_account['ava_pepper'] + $data['money'],2,".",""),
                "remark" => "充值花椒"
            ];
            $pepper_res = $this->WBusinessPepperRecordAccess->addPeppersRecord($pepper_arr);
            if(!$pepper_res)
            {
                $transaction->rollBack();
                return Tool::returnJson([],411,"增加花椒记录失败");
            }
            $transaction->commit();
            return Tool::returnJson([],200,"成功");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"充值异常".$e->getMessage());
        }



    }
}